<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\store\controller;

use library\Controller;
use library\tools\Data;
use think\Db;
use app\store\model\Common;
/**
 * 停车位
 * Class Documentlist
 * @package app\store\controller
 */
class Park extends Controller
{
    /**
     * 指定数据表
     * @var string
     */
    protected $table = 'StorePark';
    protected $table1 = 'store_park';

    /**
     * 停车位
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '停车位';
        $this->user_list = Db::table("store_member")->field("id,username")->select();
        $this->station_list = Db::table("store_power_station")->field("id,name")->select();

        $query = $this->_query($this->table)->like('name,sn')->equal("status,user_id,station_id");
        $query->where("delete_time is null")->order('id desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(&$data)
    {
        foreach ($data as &$vo) {
            $u_id = $vo['user_id'];
            $vo['user'] = Db::table("store_member")->where("id=$u_id")->find();
            $vo['img_arr'] = explode('|',$vo['images']);

            //1正常   2故障  3被占用  4停用
            if($vo['status']==1){  $vo['status_r'] = '<span style="color: green;font-weight: bold">正常</span>'; }
            if($vo['status']==2){  $vo['status_r'] = '<span style="color: red;font-weight: bold">故障</span>'; }
            if($vo['status']==3){  $vo['status_r'] = '<span style="color: orangered">被占用</span>'; }
            if($vo['status']==4){  $vo['status_r'] = '<span style="color: gray">停用</span>'; }

            $vo['station_name'] = Db::table("store_power_station")->where(["id"=>$vo['station_id']])->value("name");

        }


    }


    /**
     * 添加
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->title = '添加';
        $this->station_list = Db::table("store_power_station")->field("id,name")->select();
        $this->isAddMode = '1';
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->title = '编辑';
        $this->station_list = Db::table("store_power_station")->field("id,name")->select();
        $this->isAddMode = '0';
        $this->_form($this->table, 'form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    protected function _form_filter(&$data)
    {
        // 生成ID
        if (empty($data['id'])) $data['id'] = Db::table($this->table1)->order("id desc")->value('id')+1;
        if ($this->request->isGet()) {

        } elseif ($this->request->isPost()) {
            $data['map_image'] = $data['images'];

            $data['user_id'] = Db::table("store_power_station")->where(['id'=>$data['station_id']])->value('user_id');
            $data['create_at'] = date('Y-m-d H:i:s');

        }
    }

    /**
     * 表单结果处理
     * @param boolean $result
     */
    protected function _form_result($result)
    {
        if ($result && $this->request->isPost()) {
            $this->success('编辑成功！', 'javascript:history.back()');
        }
    }

    /**
     * 删除
     * @auth false
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        Db::table("store_device")->where(['park_id'=>$_REQUEST['id']])->update(['is_deleted'=>1,'delete_time'=>date('Y-m-d H:i:s')]);
        $this->_save("store_park", ['is_deleted'=>1,'delete_time'=>date('Y-m-d H:i:s')]);
    }



}
